<?php

namespace App\Http\Controllers;


use function gl\readable_filesize;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Validation\ValidationException;
use OSS\Core\OssException;
use OSS\OssClient;
use Symfony\Component\HttpKernel\Exception\HttpException;

class UploadController extends Controller
{

    public function ckeditor()
    {
        return $this->index();
    }

//    public function index()
//    {
//        $file        = current($_FILES);
//        $path_info   = pathinfo($file['name']);
//        $suffix      = $path_info['extension'];
//        $rename_name = sha1_file($file['tmp_name']) . '.' . $suffix;
//        $directory   = './upload/';
//        if (!is_dir($directory)) {
//            mkdir($directory, 0777, true);
//        }
//        @move_uploaded_file($file['tmp_name'], $directory . $rename_name);
//        try {
//            $oss_client = new OssClient(config('ali_oss.access_key_id'), config('ali_oss.access_key_secret'), config('ali_oss.endpoint'));
//            $res        = $oss_client->uploadFile(config('ali_oss.bucket'), $rename_name, $directory . $rename_name);
//            unlink($directory . $rename_name);
//        } catch (OssException $e) {
//            echo $e->getMessage();
//            exit;
//        }
//        return response()->json(['url' => $res['oss-request-url']]);
//    }

//    public function avatar()
//    {
//        $file = current($_FILES);
//        if ($file['size'] > (400 * 1024)) {
//            return response()->json(['status' => false, 'msg' => '头像大小不能超过' . readable_filesize(400 * 1024) . ',请换一个小点的图片做头像']);
//        }
//        $path_info   = pathinfo($file['name']);
//        $suffix      = $path_info['extension'];
//        if (!in_array($suffix,['jpg','jpeg','png','bmp'])) {
//            return response()->json(['status' => false, 'msg' => '图片格式不正确，图片只能是,jpg,jpeg,png,bmp类型的']);
//        }
//        $rename_name = sha1_file($file['tmp_name']) . '.' . $suffix;
//        $directory   = './upload/';
//        if (!is_dir($directory)) {
//            mkdir($directory, 0777, true);
//        }
//        @move_uploaded_file($file['tmp_name'], $directory . $rename_name);
//        try {
//            $oss_client = new OssClient(config('ali_oss.access_key_id'), config('ali_oss.access_key_secret'), config('ali_oss.endpoint'));
//            $res        = $oss_client->uploadFile(config('ali_oss.bucket'), $rename_name, $directory . $rename_name);
//            unlink($directory . $rename_name);
//        } catch (OssException $e) {
//            echo $e->getMessage();
//            exit;
//        }
//        return response()->json(['status' => true,'url' => $res['oss-request-url']]);
//    }

    public function avatar()
    {
        $file = current($_FILES);
        if ($file['size'] > (400 * 1024)) {
            return response()->json(['status' => false, 'msg' => '头像大小不能超过' . readable_filesize(400 * 1024) . ',请换一个小点的图片做头像']);
        }
        $path_info   = pathinfo($file['name']);
        $suffix      = $path_info['extension'];
        if (!in_array($suffix,['jpg','jpeg','png','bmp'])) {
            return response()->json(['status' => false, 'msg' => '图片格式不正确，图片只能是,jpg,jpeg,png,bmp类型的']);
        }
        $rename_name = sha1_file($file['tmp_name']) . '.' . $suffix;
        $directory   = './upload/';
        if (!is_dir($directory)) {
            mkdir($directory, 0777, true);
        }
        $res = @move_uploaded_file($file['tmp_name'], $directory . $rename_name);
        return response()->json(['status' => true,'url' => ltrim($directory . $rename_name, '.')]);
    }

    public function index()
    {

        $file      = current($_FILES);
        if ($file['size'] > (400 * 1024)) {
            return response()->json(['status' => false, 'msg' => '本站暂不支持上传大小超过400k的图片，请将图片体积压缩后再上传']);
        }
        $path_info = pathinfo($file['name']);
        $suffix    = $path_info['extension'];

        $rename_name = sha1_file($file['tmp_name']) . '.' . $suffix;
        $directory   = './upload/';
        if (!is_dir($directory)) {
            mkdir($directory, 0777, true);
        }
        $res = @move_uploaded_file($file['tmp_name'], $directory . $rename_name);
        return response()->json(['status' => true,'url' => ltrim($directory . $rename_name, '.')]);
    }
}
